package JDBC;

import java.sql.*;

public class Test02 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            // 1. 注册驱动
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            // 2. 获取连接
            String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true";
            String user = "root";
            String password = "991025";
            conn = DriverManager.getConnection(url, user, password);
            // 3. 获取数据库操作对象
            stmt = conn.createStatement();
            // 4. 执行 sql 语句
            // JDBC 的 sql 语句不能写分号, 写了报错
            String sql = "delete from user where id = 2";
            int count = stmt.executeUpdate(sql);
            System.out.println(count != 0 ? "删除成功" : "删除失败");
            sql = "update user set name = 'leilei' where id = 1";
            count = stmt.executeUpdate(sql);
            System.out.println("改变" + count + "行数据");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
